package com.cineplex.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.cineplex.role.*;

public class AdminDao {

	private static AdminDao dao = new AdminDao();
	private static DaoHelper daoHelper = DaoHelperImpl.getBaseDaoInstance();
	
	private AdminDao(){
		
	}
	public static AdminDao getInstance(){
		return dao;
	}
	
	public void save(Admin admin){
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt =null;
		try{
			stmt = con.prepareStatement("insert into admin(id,password)" +
					"values(?,?)");
			stmt.setString(1, admin.getId());
			stmt.setString(2, admin.getPassword());
			
			stmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally{
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
	}
	
	public Admin get(String id){
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		try{
			stmt = con.prepareStatement("select * from admin where id=?");
			stmt.setString(1, id);
			
			result = stmt.executeQuery();
			
			if(result.next()){
				Admin admin = new Admin();
				admin.setId(result.getString(1));
				admin.setPassword(result.getString(2));
				return admin;
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		finally
		{
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}
		return null;
	}
}
